package com.linkgie.galaxyframework.crypto.context;

import com.linkgie.galaxyframework.crypto.AsymmetricCryptoService;
import com.linkgie.galaxyframework.crypto.CryptoKeypair;
import com.linkgie.galaxyframework.crypto.PrivateKey;
import com.linkgie.galaxyframework.crypto.PublicKey;
import com.linkgie.galaxyframework.crypto.spi.AsymmetricFuncSPI;
import com.linkgie.galaxyframework.crypto.spi.AsymmetricKeypair;
import com.linkgie.galaxyframework.crypto.spi.PrivateKeybytes;
import com.linkgie.galaxyframework.crypto.spi.PublicKeybytes;

public class AsymmetricServiceBase<T extends AsymmetricFuncSPI> extends CryptoServiceBase<T> implements AsymmetricCryptoService {

	public AsymmetricServiceBase(T innerService) {
		super(innerService);
	}

	@Override
	public CryptoKeypair generateKeypair() {
		AsymmetricKeypair kp = INNSER_SERVICE.generateKey();
		PublicKey pubKey = ENCODING.encode(ALGORITHM, kp.getPublicKey());
		PrivateKey privKey = ENCODING.encode(ALGORITHM, kp.getPrivateKey());
		return new CryptoKeypair(pubKey, privKey);
	}

	@Override
	public CryptoKeypair generateKeypair(byte[] seed) {
		AsymmetricKeypair kp = INNSER_SERVICE.generateKey(seed);
		PublicKey pubKey = ENCODING.encode(ALGORITHM, kp.getPublicKey());
		PrivateKey privKey = ENCODING.encode(ALGORITHM, kp.getPrivateKey());
		return new CryptoKeypair(pubKey, privKey);
	}

	@Override
	public PublicKey generatePublicKey(PrivateKey privateKey) {
		PrivateKeybytes privKeybytes = ENCODING.decode(privateKey);
		PublicKeybytes pubKeybytes = INNSER_SERVICE.generatePublicKey(privKeybytes);
		return ENCODING.encode(ALGORITHM, pubKeybytes);
	}
}
